package com.naicheng.ivote.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.HotOptionsBean;
import com.naicheng.ivote.doman.HotShareOptionsBean;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShareOptionAdapter extends BaseAdapter {

    private Context context;
    private Handler handler;
	private LayoutInflater mInflater;
    private ArrayList<HotShareOptionsBean> list;

    /**
     * 
     * @param context
     * @param list
     */
    public ShareOptionAdapter(Context context,ArrayList<HotShareOptionsBean> list,Handler handler)
    {
    	this.context=context;
    	this.handler=handler;
    	this.list = list;
    	this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	ViewHolder	holder;
    	if(convertView == null)
    	{
    		holder = new ViewHolder();
    		convertView = mInflater.inflate(R.layout.hotitem_options_houshare_item, null);
    		holder.options_item_ll = (LinearLayout) convertView.findViewById(R.id.options_item_ll);
    		holder.options_item_tv = (TextView) convertView.findViewById(R.id.options_item_tv);
    		holder.options_item_line = (TextView) convertView.findViewById(R.id.options_item_line);
    		holder.options_item_per = (TextView) convertView.findViewById(R.id.options_item_per);
    		holder.options_item_count = (TextView) convertView.findViewById(R.id.options_item_count);
    		
    		convertView.setTag(holder);
    		convertView.setId(position);
    	}
    	else
    	{
    		holder = (ViewHolder) convertView.getTag();
    	}
    	
    	holder.options_item_tv.setText(list.get(position).getOption());
    	if(95<list.get(position).getPercent()){
    		holder.options_item_per.setText("%95以上");
    	}else if(5>list.get(position).getPercent()){
    		holder.options_item_per.setText("%5以下");
    	}else{
    		holder.options_item_per.setText("%"+list.get(position).getPercent());
    	}
    	
    	int colornum=position%5;
    	switch (colornum) {
    	case 0:
    		holder.options_item_line.setTextColor(context.getResources().getColor(R.color.option1));
    		break;
    	case 1:
    		holder.options_item_line.setTextColor(context.getResources().getColor(R.color.option2));
    		break;
    	case 2:
    		holder.options_item_line.setTextColor(context.getResources().getColor(R.color.option3));
    		break;
    	case 3:
    		holder.options_item_line.setTextColor(context.getResources().getColor(R.color.option4));
    		break;
    	}
    	int pernum=list.get(position).getPercent();
    	int linenum=(pernum*35)/100;
    	StringBuffer buf=new StringBuffer();
    	for (int i = 0; i < linenum; i++) {
    		buf.append("|");
    		holder.options_item_line.setText(buf.toString());
    		
		}
    	holder.options_item_count.setText(list.get(position).getCount()+"票");
    	
        return convertView;
    }

    public class ViewHolder
    {
    	public TextView options_item_tv;
    	public TextView options_item_line;
    	public TextView options_item_per;
    	public TextView options_item_count;
    	public LinearLayout options_item_ll;
    	
    	
    }
    
}
